package com.qifan.energy.bean.result;

import java.io.Serializable;

public class Pagination2 implements Serializable {

    public static final int MAX_LIMIT = 1000; // 单页最大
    public static final int PAGESIZE_2 = 2; // 测试2条
    public static final int PAGESIZE_5 = 5; // 测试5条
    public static final int PAGESIZE_10 = 10; // 默认10条
    public static final int PAGESIZE_20 = 20; // 20条
    public static final int PAGESIZE_50 = 50; // 50条
    public static final int PAGESIZE_100 = 100; // 100条
    private Integer pageNum = 0;
    private Integer pageSize = 10;
    private Integer total = 0;
    private Integer totalPage = 0;

    // TODO remove the following unused fields
    private String sortField = "lastUpdate";
    private String sortOrder = "DESC";

    public Pagination2() {}

    public Pagination2(Integer pageNum, Integer pageSize) {
        this.setPageNum(pageNum);
        this.setPageSize(pageSize);
    }

    public Pagination2(Pagination2 pagination) {
        if (pagination == null) {
            throw new NullPointerException("pagination is null");
        }
        this.pageNum = pagination.pageNum;
        this.pageSize = pagination.pageSize;
        this.total = pagination.total;
        this.totalPage = pagination.totalPage;
        if (pagination.getSortField() != null) {
            this.sortField = pagination.getSortField();
        }
        if (pagination.getSortOrder() != null) {
            this.sortOrder = pagination.getSortOrder();
        }
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        if (pageNum != null && pageNum >= 0) {
            this.pageNum = pageNum;
        }
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize != null && pageSize > 0) {
            if (pageSize > MAX_LIMIT) {
                pageSize = MAX_LIMIT;
            }
            this.pageSize = pageSize;
        }
    }

    public void setPage(Integer page) {
        if (page != null && page > 0) {
            this.setPageNum((page - 1) * this.pageSize);
        }
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        if (total != null) {
            this.total = total;
        }
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        if (totalPage != null){
            this.totalPage = totalPage;
        }
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    @Override
    public String toString() {
        return "Pagination [offset=" + pageNum + ", limit=" + pageSize + ", total=" + total + ", totalPage=" + totalPage + "]";
    }
}
